﻿using Xwltz.Framework.Tool;

namespace Xwltz.Framework.Mvc
{
    /// <summary>
    /// 该类提供返回前端用的大部分JSON数据方法
    /// 如果不满足需求，请在自己的项目中重写该类并继承或者直接弃用自己封装
    /// </summary>
    public class WebResultJson
    {
        private readonly JsonAdapter _adapter = new JsonAdapter();

        /// <summary>
        /// 返回执行结果
        /// </summary>
        /// <param name="success">成功or失败</param>
        /// <param name="message">信息</param>
        /// <param name="args">其他信息</param>
        /// <returns></returns>
        public virtual WebCustomJsonResult WebCustomJson(bool success, string message = "", params string[] args)
        {
            return new WebCustomJsonResult(_adapter.Command(success, message, args));
        }
        /// <summary>
        /// 返回执行结果，如果只写第一个参数，则默认FALSE
        /// </summary>
        /// <param name="message"></param>
        /// <param name="success"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual WebCustomJsonResult WebCustomJson(string message, bool success = false, params string[] args)
        {
            return new WebCustomJsonResult(_adapter.Command(success, message, args));
        }
        /// <summary>
        /// 返回执行结果，用于范围模型对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual WebCustomJsonResult WebCustomJson(object obj)
        {
            return new WebCustomJsonResult(new JsonAdapter().Detail(obj));
        }
        /// <summary>
        /// 返回执行结果，用于返回LayUI专用的分页对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="count"></param>
        /// <param name="layui"></param>
        /// <returns></returns>
        public virtual WebCustomJsonResult WebCustomJson(object obj, int count, bool layui = false)
        {
            return new WebCustomJsonResult(_adapter.Grid(obj, count, layui));
        }
        /// <summary>
        /// 返回执行结果，用于返回DataTables专用的分页对象
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="count"></param>
        /// <param name="draw"></param>
        /// <returns></returns>
        public virtual WebCustomJsonResult WebCustomJson(object obj, int count, string draw)
        {
            return new WebCustomJsonResult(_adapter.DtGrid(obj, count, draw));
        }
    }
}
